<template>
  <el-dialog title="" :visible.sync="centerDialogVisible" width="40%" @close="closeForm" center>
    <div>
      <div class="top">
        <h1>{{ruleForm.id===""?'添加':'修改'}}管理员</h1>
      </div>
      <div class="content">
        <el-form
          :model="ruleForm"
          status-icon
          :rules="rules"
          ref="ruleForm"
          label-width="100px"
          class="demo-ruleForm"
        >
          <el-form-item label="用户名:" prop="userName">
            <el-input v-model="ruleForm.userName"></el-input>
          </el-form-item>
          <el-form-item label="手机号:" prop="phone">
            <el-input v-model="ruleForm.phone"></el-input>
          </el-form-item>
          <el-form-item label="密码:" prop="pass">
            <el-input
              type="password"
              v-model="ruleForm.pass"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <!-- <el-form-item label="确认密码:" prop="checkPass">
            <el-input
              type="password"
              v-model="ruleForm.checkPass"
              autocomplete="off"
            ></el-input>
          </el-form-item> -->
          <el-form-item class="footer_btn">
            <div class="f_b">
              <el-button @click="closeForm()">取消</el-button>
            </div>
            <div class="f_b">
              <el-button type="primary" @click="submitForm('ruleForm')"
                >提交</el-button
              >
            </div>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </el-dialog>
</template>

<script>
export default {
  name: "Popup",
  props: ["editUser"],
  data() {
    var userName = (rule, value, callback) => {
      if (!value.replace(/(^\s*)|(\s*$)/g, "")) {
        return callback(new Error("用户名不能为空"));
      } else {
        callback();
      }
      // setTimeout(() => {
      //   if (!Number.isInteger(value)) {
      //     callback(new Error("请输入用户名"));
      //   } else {
      //     callback();
      //   }
      // }, 1000);
    };
    var phone = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("手机号不能为空"));
      }
      var myreg = /^[1][3,4,5,7,8][0-9]{9}$/;
      setTimeout(() => {
        if (!myreg.test(parseInt(value))) {
          callback(new Error("请输入正确的手机号"));
        } else {
          callback();
        }
      }, 1000);
    };
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        if (this.ruleForm.checkPass !== "") {
          this.$refs.ruleForm.validateField("checkPass");
        }
        callback();
      }
    };
    // var validatePass2 = (rule, value, callback) => {
    //   if (value === "") {
    //     callback(new Error("请再次输入密码"));
    //   } else if (value !== this.ruleForm.pass) {
    //     callback(new Error("两次输入密码不一致!"));
    //   } else {
    //     callback();
    //   }
    // };
    return {
      centerDialogVisible: false,
      ruleForm: {
        id: "",
        userName: "",
        phone: "",
        pass: "",
        // checkPass: "",
      },
      rules: {
        userName: [{ validator: userName, trigger: "blur" }],
        phone: [{ validator: phone, trigger: "blur" }],
        pass: [{ validator: validatePass, trigger: "blur" }],
        // checkPass: [{ validator: validatePass2, trigger: "blur" }],
      },
    };
  },
  watch: {
    centerDialogVisible(nvalue, ovalue) {
      if (nvalue && this.editUser.id) {
        this.ruleForm = {
          id: this.editUser.id ? this.editUser.id : "",
          userName: this.editUser.name ? this.editUser.name : "",
          phone: this.editUser.phone ? this.editUser.phone : "",
          pass: this.editUser.password ? this.editUser.password : "",
          // checkPass: "123456",
        };
      } else {
        this.ruleForm = this.$options.data().ruleForm;
      }
    },
  },
  methods: {
    // 提交
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$emit("addAdmin", this.ruleForm);
          this.centerDialogVisible = false;
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    // 取消
    closeForm(){
      this.$refs['ruleForm'].clearValidate()
      this.centerDialogVisible = false;
    },
  },
};
</script>
<style scoped>
/deep/ .el-dialog__body {
  padding-top: 0;
}
/* 底部按钮 */
.footer_btn {
}
.footer_btn /deep/ .el-form-item__content {
  display: flex;
  justify-content: center;
  margin-left: 0px !important;
}
</style>